package com.zidian.teacher.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.widget.ImageView;

import com.zidian.teacher.R;


/**
 * @{# LoadDealUtil.java Create on 2016-5-5 下午5:22:03
 * <p>
 * class desc: 加载数据的进度条
 * <p/>
 * </p>
 * @Version 1.0 Panca QQ643691522
 */
public class ProgressBarBlackUtil {

    private static AlertDialog dlg;
    private static AnimationDrawable frameAnim;

    /**
     * 显示对话框
     */
    public static void loadShow(final Context context) {
        dlg = new AlertDialog.Builder(context,
                R.style.global_progressbar_dialog_style).create();
        dlg.show();

        //View view = View.inflate(context, R.layout.global_load, null);
        View view = View.inflate(context, R.layout.global_load_center, null);

        dlg.setContentView(view);
        dlg.setCancelable(true);
        dlg.setCanceledOnTouchOutside(false);
        ImageView iv_frame = (ImageView) view.findViewById(R.id.ivload);


        // 通过逐帧动画的资源文件获得AnimationDrawable示例
        frameAnim = (AnimationDrawable) context.getResources().getDrawable(R.drawable.load_anim);
        // 把AnimationDrawable设置为ImageView的背景
        iv_frame.setImageDrawable(frameAnim);
        start();//开始动画
        // RotateAnimation animation =new
        // RotateAnimation(0f,360f,Animation.RELATIVE_TO_SELF,
        // 0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        // animation.setDuration(300);//设置动画持续时间
        // animation.setRepeatCount(2);//设置重复次数
        // animation.setAnimationListener(context);
        // iv.startAnimation(animation);
        /*
         * dlg.setOnCancelListener(new DialogInterface.OnCancelListener() {
		 * public void onCancel(DialogInterface dialog) { activity.finish(); }
		 * });
		 */

    }

    /**
     * 关闭对话框
     */
    public static void loadCancel() {
        if (dlg != null && dlg.isShowing()) {
            dlg.cancel();
        }
        stop();//
    }

    /**
     * 开始播放
     */
    private static void start() {
        if (frameAnim != null && !frameAnim.isRunning()) {
            frameAnim.start();
        }
    }

    /**
     * 停止播放
     */
    private static void stop() {
        if (frameAnim != null && frameAnim.isRunning()) {
            frameAnim.stop();
        }
    }
}
